/**
 * Created by alice on 2017/1/13 0013
 */

$(function () {
    $("#add").click(function () {
        $("#_cid").val("");
        document.getElementById("form").reset();
        $('#operationStatus').hide();
        $('#_operationStatus_label').hide();
        $('#summernote').summernote('code', "");
        $("#_editCourtInfo").modal("show");
    });
    //查询的开始时间默认给今天
    $('#startTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        console.log(ev.date.valueOf());
    });
    $('#endTime').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        // console.log(ev.date.valueOf());
    });
    $('#_operation_time').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        // console.log(ev.date.valueOf());
    });
    $('#_complete_time').datetimepicker({
        bootcssVer: 3,
        minView: "month", //选择日期后，不会再跳转去选择时分秒
        weekStart: "1",
        format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
        language: 'zh-CN', //汉化
        autoclose: true //选择日期后自动关闭
    }).on('changeDate', function (ev) {
        // console.log(ev.date.valueOf());
    });
    var summernote = $('#summernote');
    summernote.summernote({
        height: 300,                 // set editor height
        minHeight: null,             // set minimum height of editor
        maxHeight: null, // set maximum height of editor
        lang: 'zh-CN',
        codemirror: { // codemirror options
            theme: 'monokai'
        }
        // focus: true                  // set focus to editable area after initializing summernote
    });
    $("#sys_court_status").find("a").attr("href", r.base_path + "system_monitor/sys_court_status?courtUid=" + r.court_id);
    $("#sys_play_info").find("a").attr("href", r.base_path + "system_monitor/sys_play_info?court=Uid" + r.court_uid);
    $("#sys_meeting").find("a").attr("href", r.base_path + "views/system_monitor/sys_meeting.html?court=" + r.court_id);
    $("#sys_server_status").find("a").attr("href", r.base_path + "system_monitor/sys_server_status?courtUid=" + r.court_uid);
    $("#sys_warning_message").find("a").attr("href", r.base_path + "system_monitor/sys_warning_message?courtUid=" + r.court_uid);
    $("#sys_equipment_management").find("a").attr("href", r.base_path + "views/system_monitor/sys_equipment_management.html?court=" + r.court_id);
    $("#sys_log_info").find("a").attr("href", r.base_path + "system_monitor/sys_log_info?courtUid=" + r.court_uid);
    init();
    var panel_body = $('.panel-body');
    panel_body.focus(function () {
        var markupStr = summernote.summernote('code');
        if (markupStr == "请输入问题描述") {
            summernote.summernote('code', "")
        }
    });
    panel_body.blur(function () {
        var markupStr = summernote.summernote('code');
        if (!markupStr) {
            summernote.summernote('code', "请输入问题描述");
        }
    })

});

function init() {
    var table = $("#table").empty();
    table.bootstrapTable({
        columns: [{
            field: 'Number',
            title: '序号',
            class: 'cmp-table-right',
            width: '5.3%',
            formatter: function (value, row, index) {
                return (index + 1) > 9 ? index + 1 : '0' + (index + 1);
            }
        }, {
            field: 'courtname',
            title: '所属法院'
        }, {
            field: 'operationUser',
            title: '发起人'
//            width: "20%"
        }, {
            field: 'operationTime',
            title: '问题时间'

        },
            {
                field: 'operationStatus',
                title: '处理状态',

                formatter: function (result) {
                    return result != 0 ? result == 1 ? "正在处理" : result == 2 ? "处理完成" : result == 3 ? "已废弃" : "未知" : "待处理";
                }
            },
            {
                field: 'completeTime',
                sortName: 'complete_time',
                title: '完成时间'
            },
            {
                field: 'edit',
                title: '操作',
                class: 'center sfc_operation',
                formatter: function (value, row) {
                    return '<a href="javascript:operation_detail(\'' + row.uid + '\')"><i class="fa fa-file-text fa-lg"></i></a>&nbsp;' +
                        '<a href="javascript:operation_edit(\'' + row.uid + '\')"><i class="fa fa-edit fa-lg"></i></a>&nbsp;';
                }
            }]
    });
    table.bootstrapTable("hideLoading");
    count_operation();
    $("#loading").hide();
}
var pageSize = 12;
function count_operation() {
    var search_form = $('#search_form');
    var operationStatus = search_form.find("select[name='operationStatus']").val();
    var operationUser = search_form.find("input[name='operationUser']").val();
    var operationTime = search_form.find("input[name='operationTime']").val();
    var completeTime = search_form.find("input[name='completeTime']").val();
    var startTime = $("#startTime").find("input").val();
    var endTime = $("#endTime").find("input").val();
    if (operationStatus < 0) {
        operationStatus = null;
    }
    $.ajax({
        url: r.base_path + "state/countCourtOperation",
        type: "get",
        data: {
            operationStatus: operationStatus,
            operationUser: operationUser,
            operationTime: operationTime,
            completeTime: completeTime,
            startTime: startTime,
            endTime: endTime,
            courtId: r.court_id,
            search: r.flag_search
        },
        dataType: "json",
        success: function (result) {
            // console.log(result);
            if (result.data == 0) {
                page(1, 1, null);
            } else {
                page(1, Math.ceil(result.data / pageSize), select_operation);
            }
            select_operation(1);
        },
        error: function () {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.msg('查询发生了错误！');
            });

        }
    });
}
function select_operation(pageNo, name, order) {
    name = "operationTime";
    order = "desc";
    var search_form = $('#search_form');
    var operationStatus = search_form.find("select[name='operationStatus']").val();
    var operationUser = search_form.find("input[name='operationUser']").val();
    var operationTime = search_form.find("input[name='operationTime']").val();
    var completeTime = search_form.find("input[name='completeTime']").val();
    var startTime = $("#startTime").find("input").val();
    var endTime = $("#endTime").find("input").val();
    if (operationStatus < 0) {
        operationStatus = null;
    }
    $.get(r.base_path + "state/selectCourtOperation", {
        operationStatus: operationStatus,
        operationUser: operationUser,
        operationTime: operationTime,
        startTime: startTime,
        endTime: endTime,
        pageSize: pageSize,
        pageNo: pageNo,
        sort: order,
        order: name,
        courtId: r.court_id,
        search: r.flag_search
    }, function (result) {
        if (result && result.status == 0) {
            $('#table').bootstrapTable("load", result.data);
            $('#table').show();
        }
    }, 'json');
    $("#loading").hide();
}

function operation_detail(uid) {
    $.post(r.base_path + "state/selectCourtOperationByPrimaryKey", {
        uid: uid
    }, function (result) {
        if (result && result.status == 0) {
            $('#_operationUser').html("问题发起人：" + result.data.operationUser);
            $('#_operationContext').html(result.data.operationContext);
            $('#_operationTime').html("问题时间：" + result.data.operationTime);
            if (result.data.completeTime) {
                $('#_completeTime').html("完成时间：" + result.data.completeTime);
            } else {
                $('#_completeTime').html("完成时间：" + "-");
            }

            $("#_detail").modal("show");
        } else {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.msg('查找失败！');
            });

        }
    }, 'json')
}

function operation_edit(uid) {
    $.post(r.base_path + "state/selectCourtOperationByPrimaryKey", {
        uid: uid
    }, function (result) {
        if (result && result.status == 0) {
            var form = $('#form');
            form.find("input[name='uid']").val(result.data.uid);
            form.find("input[name='operationUser']").val(result.data.operationUser);
            form.find("input[name='operationTime']").val(result.data.operationTime);
            $('#operationStatus').show();
            $('#_operationStatus_label').show();
            form.find("select[name='operationStatus']").val(result.data.operationStatus);
            $('#summernote').summernote('code', result.data.operationContext);
            $("#_editCourtInfo").modal("show");
        } else {
            layui.use('layer', function () {
                var layer = layui.layer;
                layer.msg('查找失败！');
            });

        }
    }, 'json')
}

function update_btn() {
    var form = $('#form');
    var uid = form.find("input[name='uid']").val();
    var operationUser = form.find("input[name='operationUser']").val();
    var operationTime = form.find("input[name='operationTime']").val();
    var operationStatus = $('#operationStatus').val();
    var completeTime = form.find("input[name='completeTime']").val();
    var markupStr = $('#summernote').summernote('code');
    if (!operationUser || !operationStatus) {
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.msg('请补充完整数据！');
        });
        return;
    }
    if (uid) {
        $.post(r.base_path + "state/updateCourtOperation", {
            uid: uid,
            operationUser: operationUser,
            operationStatus: operationStatus,
            operationTime: operationTime,
            completeTime: completeTime,
            operationContext: markupStr
        }, function (result) {
            if (result && result.status == 0) {
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.msg('更新成功');
                });
                $("#_editCourtInfo").modal("hide");
                count_operation();
            } else {
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.msg('更新失败！');
                });
                count_operation();
            }
        }, 'json')
    } else {
        $.post(r.base_path + "state/insertCourtOperation", {
            operationUser: operationUser,
            operationTime: operationTime,
            completeTime: completeTime,
            operationContext: markupStr,
            courtId: r.court_id
        }, function (result) {
            if (result && result.status == 0) {
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.msg('添加成功');
                });
                $("#_editCourtInfo").modal("hide");
                count_operation();
            } else {
                layui.use('layer', function () {
                    var layer = layui.layer;
                    layer.msg('添加失败！');
                });
                count_operation();
            }
        }, 'json')
    }

}

function page(now, totalPages, callback) {
    base.pagination('#_page', {
        currentPage: now,
        totalPages: totalPages,
        onPageClicked: function (page) {
            callback(page);
        }
    });
}